from psqloperation import PsqlCon
import chromeDeob
import jsbeautify
import rmcomment
import chunkJS
import malurl
import utils

import sys


class Deobfuscator:
	def deobLink(self, url, jscommentname, jsname, chunkoutdir, beautifydir):
		chromeDeob.chromeDeobLink(url, jscommentname)
		# rmcomment.rmCommentFile(jscommentname, jsname)
		chunkJS.chunkJSFile(jscommentname, chunkoutdir)
		jsbeautify.jsdeobfuscatedir(chunkoutdir, beautifydir)


	def deobTestLink(self, url):
		paras = utils.genTestName(url)
		self.deobLink(paras[0], paras[1], paras[2], paras[3], paras[4])

	def deobSpecificURL(self, url, filename):
		thefile = open(filename, 'r')
		for line in thefile:
			paras = utils.genTempnameLine(line)
			if paras[0] == url:
				self.deobLink(paras[0], paras[1], paras[2], paras[3], paras[4])


	def deobFile(self, filename):
		log = utils.LOG()
		progress = log.STATUS(filename)
		thefile = open(filename, 'r')
		count = 0
		for line in thefile:
			count = count + 1
			if count <= progress:
				continue
			print line
			paras = utils.genTempnameLine(line)
			self.deobLink(paras[0], paras[1], paras[2], paras[3], paras[4])
			log.UPDATE(filename, count)


	def deobFiles(self, regex, order = -1):
		# deobfuscate files that match regex and generate output to ../../data/JSAdd2Tree
		files = utils.lsresult(regex)
		num_files = len(files)
		if order == -1:
			for i in range(num_files-1, -1, -1):
				print 'Processing file: ' + files[i]
				self.deobFile(files[i])
		elif order == 1:
			for i in range(0, num_files):
				print 'Processing file: ' + files[i]
				self.deobFile(files[i])
		

	def deobLabel(self, label):
		self.con = PsqlCon()
		res = self.con.psqlSelectOneLabel('TEST')
		for url, url_hash, label in res:
			paras = utils.genTempnameTuple(url, url_hash, label)
			self.deobLink(paras[0], paras[1], paras[2], paras[3], paras[4])


	def deobTerry(self):
		malurl.formatFile('../../data/JSTerry/javascript_20131018/terryurl', '../../data/TempData/URLs/terryurl', '../../data/JSTerry/javascript_20131018/')
		self.deobFile('../../data/TempData/URLs/terryurl')
		# jsbeautify.deobJSTerry()


if __name__=='__main__':
	'''
	con = PsqlCon()
	print con.psqlSelectOneLabel('TEST')
	# deobLabel('TEST')
	
	# Store the related information into 
	con.psqlReadURLHashFromFile('../../data/TempData/URLs/terryurl')
	'''

	deob = Deobfuscator()
	# deobfuscate one file and generate output to ../../data/JSAdd2Tree
	# deal with the malicious ones
	# regex = '../../data/TempData/URLs/malurl*'
	# deob.deobFiles(regex, -1)

	# deal with the benign ones
	argc = len(sys.argv)
	if argc == 3:
		job = sys.argv[1]
		if job.lower() == 'test':
			url = sys.argv[2]
			deob.deobTestLink(url)
		elif job.lower() == 'train':
			regex = sys.argv[2]
			deob.deobFiles(regex, 1)
		else:
			print 'Error: This option is invalid.'
	elif argc == 1:
		# regex = '../../data/TempData/URLs/benignurl-2013-11-14-[0-9]'
		regex = '../../data/TempData/URLs/yacin-benign-[0-9]'
		deob.deobFiles(regex, 1)
	else:
		print 'Error: wrong format python $option $para'


	# Following is Deprecated and not accurate
	'''
	For examples from Terry, 21 of them cannot be compiled. 26 of them are deobfuscated through modified chrome, 8 of them are deobfuscated through modified v8.
	There are 34 valid examples out of 56 examples.
	Out of the 21 script that cannot be compiled, 3 of them are just beautified. The rest are not used.
	'''
	'''
	# deobfuscate examples from terry and generate output to ../../data/JSAdd2Tree
	# Using modified chrome
	deob.deobTerry()
	deob.deobSpecificURL('t.co', '../../data/TempData/URLs/alexaustop')
	deob.deobSpecificURL('ruian.gtisc.gatech.edu/Terry/showjs.php?jsname=jusosuwe.org_94679f9ad95828ff81c1b4951e72c794_53', '../../data/TempData/URLs/terryurl')
	
	# Using modified v8
	utils.D8Terry()
	'''
